package com.ruike.alisurface.https;

import android.text.TextUtils;

import com.ruike.alisurface.bean.ShopInfoBean;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_http.ApiTools;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.SharedUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruike.alisurface.utils.ShareKey.BASEUSERID;

public class ShopApiHttp {
    static String Url_debug = "rk_medicine_api_test";// 测试服地址
    static String Url_release = "rk_medicine_api";

    static String ShopUrl = "https://rk.shouhuojiyun.com/" + Url_release + "/android/";//正式服问诊主接口

//    static String ShopUrl = "http://robot.shouhuojiyun.com/rk_medicine_api/android/";//测试服问诊主接口

    //    测试服地址   https://rk.shouhuojiyun.com/rk_medicine_api_test/
  public   static String ocr_Idcard = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";//身份证识别地址

    public static String get_product_type = "get_product_type";//获取商品类型
    public static String get_product = "get_product";//获取商品列表
    public static String create_order = "create_order";//创建订单
    public static String check_true_name_order = "check_true_name_order";//检测实名认证状态
    public static String handle_true_name_order = "handle_true_name_order";//发起实名认证
    public static String get_medicine_order_details = "get_medicine_order_details";//获取订单详情信息
    public static String handle_remote_order = "handle_remote_order";//处理远程问诊订单
    public static String get_pay_info = "get_pay_info";//问诊通过前往支付
    public static String update_medicine_order_pic = "update_medicine_order_pic";//上传处方药图片
    public static String remote_update_card_info = "remote_update_card_info";//远程问诊订单更新身份证信息

    public static String get_print_ticket_date = "get_print_ticket_date";//打印小票信息
    public static String get_qualification_details = "get_qualification_details";//运营资质详情


    public static String get_medicine_details = "get_medicine_details";//获取商品详情

    public static String check_product_need_doctor = "check_product_need_doctor";//检测商品是否需要医生编码弹框

    public static String check_doctor = "check_doctor";//检测商品是否需要医生编码弹框

    public static String query_order_sult_state = "query_order_sult_state";//轮询问诊订单信息
    public static String get_doctor_code_pic_detail = "get_doctor_code_pic_detail";//获取医生二维码图片
    public static String set_order_state_cancel = "set_order_state_cancel";//将问诊订单标记为取消

    public static String query_doctor_online = "query_doctor_online";//检测有没有在线医生
    public static String query_proc_order_state = "query_proc_order_state";//获取订单相关状态

    public static String get_mch_logo = "get_mch_logo";// 获取logo图片

    static String mchUrl = "http://rk.shouhuojiyun.com/rk_medicine_api/mch/";


    public static String get_mch_max_sale_count = "get_mch_max_sale_count";// 获取机器单次购买上限


    public static String set_mch_max_sale_count = "set_mch_max_sale_count";// 设置机器单次购买上限


    public static String get_external_api_parameter = "get_external_api_parameter";//获取外部接口调用参数

    /**
     * 获取商品类型
     *
     * @param mApiListener
     */
    public static void get_product_type(ApiListener mApiListener) {
        ;
        Map map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        ApiTools.getInstance().get(ShopUrl + get_product_type, map, mApiListener);
    }

    /**
     * 根据类型获取药品详情
     *
     * @param type_lv1     类型级别
     * @param type_lv2
     * @param type_lv3
     * @param mApiListener
     */
    public static void get_product(String type_lv1, String type_lv2, String type_lv3, ApiListener mApiListener) {

        Map map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));

        if (!TextUtils.isEmpty(type_lv1)) {
            map.put("type_lv1", type_lv1);
        }
        if (!TextUtils.isEmpty(type_lv1)) {
            map.put("type_lv2", type_lv2);
        }
        if (!TextUtils.isEmpty(type_lv1)) {
            map.put("type_lv3", type_lv3);
        }
        ApiTools.getInstance().get(ShopUrl + get_product, map, mApiListener);
    }

    /**
     * 创建订单
     *
     * @param carShopList  药品信息
     * @param doctor_id    医生信息
     * @param mApiListener
     */
    public static void create_order(List<ShopInfoBean.DataBean> carShopList, String doctor_id, ApiListener mApiListener) {
        try {
            JSONObject orderObject = new JSONObject();
            JSONArray product_list = new JSONArray();
            for (ShopInfoBean.DataBean dataBean : carShopList) {
                JSONObject productobject = new JSONObject();
                productobject.put("productid", dataBean.getProductid());
                productobject.put("quantity", dataBean.getQuantity());
                JSONArray slotIndexs = new JSONArray();
                for (int slotIndex : dataBean.getSlotIndexs()) {
                    slotIndexs.put(slotIndex);
                }
                productobject.put("slotIndexs", slotIndexs);
                product_list.put(productobject);
            }
            orderObject.put("mch_id", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
            orderObject.put("doctor_id", doctor_id);
            orderObject.put("product_list", product_list);
            ApiTools.getInstance().post(ShopUrl + create_order, orderObject.toString(), mApiListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建订单
     *
     * @param is_no_order  是否无订单开始创建远程问诊
     * @param mApiListener
     */
    public static void create_order(boolean is_no_order, ApiListener mApiListener) {
        try {
            JSONObject orderObject = new JSONObject();
            JSONArray product_list = new JSONArray();

            orderObject.put("mch_id", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
            orderObject.put("is_no_order", is_no_order);
            orderObject.put("product_list", product_list);
            ApiTools.getInstance().post(ShopUrl + create_order, orderObject.toString(), mApiListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检测实名认证状态
     *
     * @param order_id     订单ID
     * @param mApiListener
     */
    public static void check_true_name_order(String order_id, ApiListener mApiListener) {
        Map map = new HashMap();
        map.put("order_id", order_id);
        ApiTools.getInstance().get(ShopUrl + check_true_name_order, map, mApiListener);
    }


    /**
     * 发起实名认证
     *
     * @param json         需要的参数
     * @param mApiListener
     */
    public static void handle_true_name_order(String json, ApiListener mApiListener) {

        ApiTools.getInstance().post(ShopUrl + handle_true_name_order, json, mApiListener);

    }

    /**
     * 处理远程问诊订单
     *
     * @param json         需要的参数
     * @param mApiListener
     */
    public static void handle_remote_order(String json, ApiListener mApiListener) {

        ApiTools.getInstance().post(ShopUrl + handle_remote_order, json, mApiListener);

    }

    /**
     * 订单详情信息
     *
     * @param order_id     订单id
     * @param mApiListener
     */
    public static void get_medicine_order_details(String order_id, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("order_id", order_id);
        ApiTools.getInstance().get(ShopUrl + get_medicine_order_details, map, mApiListener);

    }

    /**
     * 处方图片的上传
     *
     * @param file         文件
     * @param mApiListener
     */
    public static void get_medicine_upload(File file, ApiListener mApiListener) {
        String ShopUrl = "http://120.78.219.34:8018/tools/upload?type=medicine";
        HashMap map = new HashMap();
        map.put("file", file);
        ApiTools.getInstance().post(ShopUrl, map, mApiListener);

    }

    /* 问诊通过前往支付
     * @param order_id      订单id
     * @param mApiListener
     */
    public static void get_pay_info(String order_id, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("order_id", order_id);
        ApiTools.getInstance().get(ShopUrl + get_pay_info, map, mApiListener);

    }


    /* 订单详情信息
     * @param order_id     需要的参数
     * @param mApiListener
     */
    public static void update_medicine_order_pic(String order_id, List<String> pic_list, ApiListener mApiListener) {
        JSONObject jsonObject = new JSONObject();
        try {

            jsonObject.put("order_id", order_id);
            JSONArray jsonArray = new JSONArray();
            for (String ShopUrlpath : pic_list) {
                jsonArray.put(ShopUrlpath);
            }
            jsonObject.put("pic_list", jsonArray);
            ApiTools.getInstance().post(ShopUrl + update_medicine_order_pic, jsonObject.toString(), mApiListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /* 远程问诊订单更新身份证信息
     * @param order_id     需要的参数
     * @param mApiListener
     */
    public static void remote_update_card_info(String json, ApiListener mApiListener) {
        ApiTools.getInstance().post(ShopUrl + remote_update_card_info, json.toString(), mApiListener);
    }

    /* 获取打印小票数据
     * @param order_id
     * @param mApiListener
     */
    public static void get_print_ticket_date(String order_id, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("order_id", order_id);
        ApiTools.getInstance().get(ShopUrl + get_print_ticket_date, map, mApiListener);
    }


    /* 运营资质详情
     * @param mApiListener
     */
    public static void get_qualification_details(ApiListener mApiListener) {

        HashMap map = new HashMap();
        map.put("rk_baseuserid", SharedUtils.getInstance().getString(BASEUSERID));
        ApiTools.getInstance().get(ShopUrl + get_qualification_details, map, mApiListener);
    }

    /* 获取商品详情
     * @param product_id
     * @param mApiListener
     */
    public static void get_medicine_details(String product_id, String price, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("product_id", product_id);
        map.put("price", price);
        ApiTools.getInstance().get(ShopUrl + get_medicine_details, map, mApiListener);
    }

    /* 检测商品是否需要医生编码弹框
     * @param product_ids
     * @param mApiListener
     */
    public static void check_product_need_doctor(List<String> product_ids, ApiListener mApiListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            for (String str : product_ids) {
                jsonArray.put(str);
            }
            jsonObject.put("product_ids", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiTools.getInstance().post(ShopUrl + check_product_need_doctor, jsonObject.toString(), mApiListener);
    }


    /* 检测医生编码
     * @param card_no
     * @param mApiListener
     */
    public static void check_doctor(String card_no, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        map.put("card_no", card_no);
        ApiTools.getInstance().get(ShopUrl + check_doctor, map, mApiListener);
    }


    /* 轮询问诊订单信息
     * @param orderid
     * @param mApiListener
     */
    public static void query_order_sult_state(String orderid, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("orderid", orderid);
        ApiTools.getInstance().get(ShopUrl + query_order_sult_state, map, mApiListener);
    }


    /* 获取医生二维码图片
     * @param doctor_id 医生ID
     * @param mApiListener
     */
    public static void get_doctor_code_pic_detail(String doctor_id, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("doctor_id", doctor_id);
        ApiTools.getInstance().get(ShopUrl + get_doctor_code_pic_detail, map, mApiListener);
    }


    /* 将问诊订单标记为取消
     * @param orderid 订单ID
     * @param mApiListener
     */
    public static void set_order_state_cancel(String orderid, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("orderid", orderid);
        ApiTools.getInstance().get(ShopUrl + set_order_state_cancel, map, mApiListener);
    }


    /* 获取订单相关状态
     * @param orderid 订单ID
     * @param mApiListener
     */
    public static void query_proc_order_state(String orderid, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("orderid", orderid);
        ApiTools.getInstance().get(ShopUrl + query_proc_order_state, map, mApiListener);
    }


    /* 检测有没有在线医生
     * @param mApiListener
     */
    public static void query_doctor_online(ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("mch_id", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        ApiTools.getInstance().get(ShopUrl + "query_doctor_online", map, mApiListener);
    }

    /*  获取logo
     * @param card_no
     * @param mApiListener
     */
    public static void get_mch_logo(ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        ApiTools.getInstance().get(ShopUrl + get_mch_logo, map, mApiListener);
    }

    /*  设置机器单次购买上限
     * @param card_no
     * @param mApiListener
     */
    public static void set_mch_max_sale_count(String set_sale_count, ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        map.put("set_sale_count", set_sale_count);
        ApiTools.getInstance().get(mchUrl + set_mch_max_sale_count, map, mApiListener);
    }


    /* 获取机器单次购买上限
     * @param card_no
     * @param mApiListener
     */
    public static void get_mch_max_sale_count(ApiListener mApiListener) {
        HashMap map = new HashMap();
        map.put("mchid", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        ApiTools.getInstance().get(mchUrl + get_mch_max_sale_count, map, mApiListener);
    }

    /**
     * @param api_type 外部接口类型
     *                 阿里云身份证OCR = 1,  腾讯云实时音视频TRTC = 2,
     */
    public static void get_external_api_parameter(int api_type) {
        HashMap map = new HashMap();
        map.put("mch_id", SharedUtils.getInstance().getString(ShareKey.MCH_ID));
        map.put("api_type", api_type);
        ApiTools.getInstance().get(ShopUrl + get_external_api_parameter, map, new ApiListener() {
            @Override
            public void onRequestStart(String requestUrl, int requestId) {
                L.i("开始查询设置 第三方接口配置", requestUrl);
            }

            @Override
            public void onRequestComplete(String requestUrl, int requestId) {

            }

            @Override
            public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
                L.i("开始查询设置 第三方接口配置",   api_type==1?"阿里云身份证OCR":"腾讯云实时音视频TRTC",
                        "数据的一些配置 ==" + requestJson.toString());
                try {

                    JSONObject data = requestJson.getJSONObject("data");
                    JSONObject api_info = data.getJSONObject("api_info");
                    if (api_type == 1) {
                        SharedUtils.getInstance().putString(ShareKey.APPCODE,
                                api_info.getString("appcode"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
                L.i("开始查询设置 第三方接口配置  ",
                        api_type==1?"阿里云身份证OCR":"腾讯云实时音视频TRTC",
                        "异常=="+errorMsg,resultData);
            }
        });
    }
}
